#!/usr/bin/perl

print "Gb tool analyser. \n";

open INPUT, "<", "log.txt" or die $!;
#open $fin, "<", "log.txt" or die $!;
open $fout, ">", "output.txt";

$line_number = 0;
$line = 1;
@array;

@array = <INPUT>;

close ($fin);

print "Enter the NSEI: ", "\n";
$NSEI = <>;
chomp ($NSEI);
print $NSEI, "\n";

%alarm_hash;
@array_alarm;

for ($i = 0; $i < scalar(@array); $i++)	
{
	chomp (@array[$i]);
	if (@array[$i] =~ /$NSEI/i and (@array[$i-2] =~ /ALARM/ or @array[$i-2] =~ /CANCEL/) and @array[$i-1] =~ /3025/ )
	{
#		print @array[$i-3], "\n";
		@string1 = split /\s+/,@array[$i-3];
		$alarm_hash{type} = @string1[1];
		$alarm_hash{exchange} = @string1[2];
		$alarm_hash{computer} = @string1[3];
		$alarm_hash{equipment} = @string1[4];
		$alarm_hash{date} = @string1[5];
		$alarm_hash{hour} = @string1[6];

#		print @array[$i-2], "\n";
		@string2 = split /\s+/,@array[$i-2];

		$alarm_hash{urgency} = @string2[0];
		$alarm_hash{printout} = @string2[1];
		$alarm_hash{object} = @string2[2];
		$alarm_hash{position} = @string2[3];
		$alarm_hash{issuer} = @string2[4];

#		print @array[$i-1], "\n";
#		print $fout @array[$i-1], "\n";
		@string3 = split /\s+/,@array[$i-1];

		$alarm_hash{consecutive_num} = @string3[1];
		$alarm_hash{alarm_number} = @string3[2];
		$alarm_hash{text} = @string3[3].@string3[4].@string3[5].@string3[6].@string3[7].@string[8].@string[9];

#		push @array_alarm, alarm_hash;		


		print @array[$i], "\n";
#		print $fout @array[$i], "\n";
		@string4 = split /\s+/,@array[$i];

		$alarm_hash{psei} = @string4[1];
		$alarm_hash{nsei} = @string4[2];
		$alarm_hash{pcu_id} = @string4[3];
		$alarm_hash{nsvci} = @string4[4];
		$alarm_hash{fr_bearer_ch} = @string4[5];
		$alarm_hash{ip_adr} = hex(@string4[6]).".".hex(@string4[7]).".".hex(@string4[8]).".".hex(@string4[9]);
		$alarm_hash{port} = hex(@string4[22]);
		push @array_alarm, alarm_hash;		
	}
}
close ($fout);

print "Alarm   PSEI  NSEI    BCSU      PCU         Date          Time           IP              Port \n";
for ($index = 0; $index < scalar (@array_alarm); $index++)
{
	print $alarm_hash{alarm_number},"    ",$alarm_hash{psei},"    ",$alarm_hash{nsei},"    ",$alarm_hash{object},"    ",$alarm_hash{issuer},"    ",$alarm_hash{date},"    ",$alarm_hash{hour},"    ",$alarm_hash{ip_adr},"    ",$alarm_hash{port},"\n";

#	print $alarm_hash{type},"\t",$alarm_hash{computer},"\t",$alarm_hash{date},"\t",$alarm_hash{hour},"\n";
#	print $alarm_hash{printout},"\t",$alarm_hash{object},"\t",$alarm_hash{issuer},"\n";
#	print $alarm_hash{alarm_number},"\t",$alarm_hash{text},"\n";
#	print $alarm_hash{psei},"\t",$alarm_hash{nsei},"\t",$alarm_hash{pcu_id},"\t",$alarm_hash{nsvci},"\t",$alarm_hash{ip_adr},"\t",$alarm_hash{port},"\n";
	
}

